/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;


import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;

/**
 *
 * @author berthise
 */        // TODO code application logic here
public class Connection extends Thread {

    private static final int CONNECTION = 0;
    private static final int DECONNECTION = 1;
    private static final int MESSAGE = 2;
    private static final int ERROR = 3;
    private String idConnection;
    private DataOutputStream sortie;
    private DataInputStream entree;
    private Server server;
    private boolean alive;

    public Connection(Server server, String id, DataOutputStream sortie, DataInputStream entree) {
        this.idConnection = id;
        this.sortie = sortie;
        this.entree = entree;
        this.server = server;

        Message mess = new Message(MESSAGE, "server", "systemPanel", "La connexion est OK");
        mess.sendMessage(sortie);
        System.out.println("Connection de : " + id);
        alive = true;
    }

    /*
     * fermeture du thread en passant le alive a faux (boucle while)
     * fermeture des flux d'entree sortie
     * renvois vrai si tout est bon
     */
    public boolean closeConnection() {
        try {
            entree.close();
            sortie.close();
            alive = false;
            System.out.println("hello");
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    public void run() {
        while (alive) {
            try {
                // System.out.println("error");
                receiveMessage();
                try {
                    sleep(50);
                } catch (InterruptedException e) {
                }
            } catch (Exception ex) {
                System.out.println("erxdscdfror");
                server.closeClientConnection(idConnection);
            }
        }
    }

    /*
     * redirige le message sur le flu de sortie de la connection
     */
    public void redirectionMessage(Message mess) {
        mess.sendMessage(sortie);
    }

    /*
     * methode appller par la boucle du thread
     * si c'est pour une deconnection, elle transmet au Serveur
     * Sinon, redirection ver la gestion des communications
     */
    public void receiveMessage() {
        Message mess = new Message();
        mess.readMessage(entree);
        if (mess.getType() == DECONNECTION) {
            server.closeClientConnection(idConnection);
        } else if (mess.getType() == MESSAGE) {
            redirectionMessage(mess);
        }
    }

    /**
     * getteur est setteur
     * @return
     */
    public String getIdConnection() {
        return idConnection;
    }

    public void setIdConnection(String idConnection) {
        this.idConnection = idConnection;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Connection other = (Connection) obj;
        if ((this.idConnection == null) ? (other.idConnection != null) : !this.idConnection.equals(other.idConnection)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + (this.idConnection != null ? this.idConnection.hashCode() : 0);
        return hash;
    }
}
